<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <1050429284@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-5-21
*   +----------------------------------------------------------------------
*   | Link (  )
*   +----------------------------------------------------------------------
**/

/**
 * 修改价格 获取价格区间  类
 *
 */
class  AlterPrice {
	protected $product, $tags, $description, $tagsObj,  $brand, $jiayi, $price, $PriceList;
	
	public function __construct(){
		$this->product = M( 'Product' );
		$this->tags    = M('Tags');
		$this->tagsObj = M('TagsObj');
		$this->brand   = M('ProductBrand');
		$this->description = M('ProductDescription');
		$this->price   = M('productPriceO2o');
		$this->jiayi   = M('ProductPriceB2b');
		$this->PriceList= M('ProductPriceList');
		$this->producto2o = M('Product_o2o');
	}
	
	
	//根据关键字获取产品IDs  传空返回空
	public function getFromDes($key='' ){
		  $tab = 	$this->description;
		  if(""==$key) return '';
		  else $map['name'] = array('like','%'.$key.'%');
		  //添加更多条件
////////////////////////////////////////////////////////////		  
		  $ida     = $tab ->where($map)->field('product_id') ->select();
		  $i = 0; $ids =array();
		  foreach($ida as $v){
		  	 $ids[$i++] = $v['product_id'];
		  }
		  return $ids;
	}
	
	//根据品牌字段获取产品IDs  传空返回空
 	public function getFromBrd($key=''){
		   if(""==$key) return '';
		   else $map['name'] = array('like','%'.$key.'%');
			  //添加更多条件   
		   //////////////////////////////////////////////////////////////////////////
		  $id_brands = $this->brand->where($map)->field('id') ->select();
		  $i=0; $ids=array();
		  foreach($id_brands as $id_brand){
		  	  $map['brand_id'] = $id_brand['id'];
			  $ida = $this->product->where($map) ->field('id')->select();
			  foreach($ida as $v){
				  $ids[$i++] = $v['id'];
			   }
		  }
		  return $ids;
	}
	
	//根据标签字段获取产品IDs     传空返回空
	public function getFromTag($tags_id){
	      if(""==$tags_id) return '';
			   $map['tags_id'] = $tags_id;
			   $map['type']    = '1';
			   $ida =$this->tagsObj->where($map) ->field('obj_id') ->select();
			   $i=0 ; $ids =array();
			   foreach( $ida as $v){
				     $ids[$i++] = $v['obj_id']; 		   
			   }
		  return $ids;
	}	
	
	//综合各种检索条件  关键字 品牌 标签    获取IDs  并且 按要求排列顺序 (升序)
	// 
	//$key('keywords'=>,'brand'=>,'tag'=>)
	public function getIds($key){
		$ids = array();
 		$idks=  $this->getFromDes($key['keywords'] );
 		$idbs=  $this->getFromBrd($key['brand'] );
 		$idts=  $this->getFromTag($key['tag_id']);

		if(is_array($idks)) array_push($ids,$idks);
		if(is_array($idbs)) array_push($ids,$idbs);
		if(is_array($idts)) array_push($ids,$idts);
		if(!$ids) return '';
		$id = $ids[0];
		for($i=1 ; $i<count($ids); $i++){
			$id = array_intersect($id,$ids[$i]);
		}		
		return $id; 
		
	}
	
	//根据产品ID  基础价价格 增量 浮动  保存到 数据库  并返回利润    传非法 返回空
	//$key  为数组 元素分别为 :  1.产品ID   2.基础价格    3.固定增量     4.增长百分比    返回: 利润 或者错误值
	// $key{'add'=>,'percent'=>,'id'=>,'price'=>}       
 	public function setPrice($key){
		  if(!is_array($key) ||  count($key)!=4 ) return '';
		  if($key['add'] != '' && $key['percent'] !='') return '';
		  
		  //先获根据产品ID取供货价格
		  $map['product_id'] = $key['id'];  
		  $supply = $this->jiayi ->where($map) ->field('jiayi') ->select();
		  if($supply==NULL) $supply = 0;
		  
		  //将信息保存到数据库
		  $data['add'] = $key['add'];	
	      $data['percent'] = $key['percent'];
		  $data['price'] = $key['price'];
		  $result = $this->price ->where($map) ->save($data);
		  if($result == false){
		 		 dump('数据更新失败！');
		  }
		  //返回利润
		  
		  if($key['add'] != ''){
		  		return $lirun = $key['price'] + $key['add'] -$supply;
		  }else {
			  return $lirun = $key['price']*(1+$key['percent']/100) - $supply;
		  }
		 
		  
 	}
 	
 	//产品价格列表
 	public  function proPriceList($user_id ,$pro_ids = array(),$status,$limit=10){			
 		$map['po.user_id'] = $user_id;
 		
		if($pro_ids){
			$map['po.product_id'] = array('in',$pro_ids);
		}
		$map['po.status'] = $status ? $status : array('NEQ',3);

		$list = $this->producto2o
		->join(' as po left join product_price_o2o as ppo ON po.product_id = ppo.product_id and po.user_id = ppo.user_id' )
		->join(' left join product_price_b2b as ppb ON po.product_id = ppb.product_id' )
		->join(' left join product_description as pd ON po.product_id = pd.product_id' )
		->where($map)
		->field('po.id,po.user_id,po.status,po.product_id,round(ppo.price/100) as price,ppo.add,ppo.percent,round(ppb.jiayi/100) as jiayi,pd.name')
		->limit($limit)->select();

 		//计算当前价格，和利润
 		foreach ($list as $k=>$vo){
 			$profit = $c_price = 0;		
 			
 			
 			if($vo['price']>0){
 				$profit = (int)$vo['price']-(int)$vo['jiayi'];
 				$c_price = $vo['price'];
 			}
			elseif($vo['add']>0){
				$profit = (int)$vo['add'];
				$c_price = (int)$vo['add']+(int)$vo['jiayi'];
			}
			elseif($vo['percent']>0){
				$profit = (int)$vo['jiayi']*((int)$vo['percent']/100);
				$c_price = (int)$vo['jiayi']+(int)$profit;
			}else{
				$profit = (int)$vo['jiayi'];
				$c_price = (int)$vo['jiayi'];
			}
 			$list[$k]['profit'] = round($profit);
 			$list[$k]['c_price'] = round($c_price);
 		}
 		return $list;
 	}
 	

 	/**
 	 * 上架过滤 ： 刷选满足上架条件的商品（ 必选设置价格 ： price >0 or `add` >0 or percent >0   以及 采购价 jiayi <0）
 	 * 
 	 * */
 	public function sj_filter($user_id,$pro_ids){
 		//刷选B2B 采购价格 >0 的 产品id
 		$dao = M('Product');
 		$map['id'] = array('IN',$pro_ids);
 		$map['price'] = array('EGT',1);
 		$product = $dao->field('id')->where($map)->select();
 		foreach($product as $vo){
 			$pids[] = $vo['id'];
 		}
 		$pro_ids = implode(',', $pids);
 		
 		if($pro_ids){
	 		$model = M();
	 		$sql = 'select product_id from product_price_o2o where user_id = '.$user_id.' and ( price >0 or `add` >0 or percent >0 ) and product_id in ('.$pro_ids.')';
	 		$res = $model->query($sql);
	 		foreach($res as $r){
	 			$arr[]=$r['product_id'];
	 		}
 		}
 		return $arr; 		
 	}
	
	//根据ID 获取供货价
	public function getJiayi($Id){
		$map['product_id'] = array('EQ',$Id);
		$jia = $this->jiayi->where($map) ->field('jiayi') ->select();
		return $jia[0]['jiayi'];
	}
	//根据ID 获取 基础价  增长   百分比
	public function getPap($Id){
		$map['product_id'] = array('EQ',$Id);
		$res = $this->price ->where($map)->field('price,add,percent')->select();	
		return $res[0];
	}
	

}